/**
 *  物品详情商品收藏
 */
define(function(require,exports,module) {
    var myPub = require('./public');

    GoodsCollection();
    function  GoodsCollection() {
        var bOff = true;
        var showTipTime = null;
        var oBtn_collection = $('.js_btn_collection ');   // 收藏按钮
        var oTip_collection = $('.js_tip_collection ');   // 收藏提示
        oBtn_collection.on('click', function (e) {
            if(bOff) {
                bOff = false;
                var _url = '';
                var _off = '';
                if($(this).hasClass('active')) {
                    //_url = "/shop/goodsCollection/cancelCollection";
                    _url = "/m/g/goods/collection/1.0/collectGoods?flag=false";
                    _off = false;
                } else {
                    //_url = "/shop/goodsCollection/addCollection";
                    _url = "/m/g/goods/collection/1.0/collectGoods?flag=true";
                    _off = true;
                }

                G_JSON_POST(_url, {"goodsId":$(this).attr('goodsId')},function(data){
                    //if(data.status == "success"){
                    if(data.ret.code == "0"){
                        ShowTip(_off);
                        if(_off) {
                            // 收藏成功
                            oBtn_collection.addClass('active');
                            if( typeof setActionData != 'undefined' ) {
                                userAction($(this), setActionData);
                            }
                        }
                        else {
                            // 取消收藏
                            oBtn_collection.removeClass('active');
                        }
                    }
                }, function () {}, function () {
                    clearTimeout(showTipTime);
                    showTipTime = setTimeout(function () {
                        ShowTip(false);
                        bOff = true
                    }, 2000);
                });

            }

            e.stopPropagation();
        })

        function ShowTip(off) {
            if(off) {
                oTip_collection.show().stop().animate({
                    'opacity': 1
                },500);
            }
            else {
                oTip_collection.stop().animate({
                    'opacity': 0
                },500, function () {
                    oTip_collection.hide();
                });
            }
        }

        // 添加用户行为（仅收藏）
        function userAction(obj, keyword) {
            ddjhoc.setData(obj, 'collect', 'add', function () {
                console.log('params.collect', ddjhoc.params);
                ddjhoc.sendData( ddjhoc.params );
            }, keyword);
        }

    }

    exports.GoodsCollection = GoodsCollection;
});